package models;

import java.util.ArrayList;
import java.util.Random;

public class Measure 
{
	public String content;
	
	public void generateMeasures(Track newTrackModel, int instrumentNum, int octave, ArrayList<Double> prevLengths, ArrayList<Integer> prevValues)
	{
		Random random = new Random();
		final int SEMI_TONE_COUNT = 12;
		
		int[] powersOfTwo = new int[]{2, 4, 8, 16};
		int[] harmonicValues = new int[]{3, 5, 8};
		int[] naturalNoteValues = new int[]{0, 2, 4, 5, 7, 9, 11};
		
		int noteValue;
		double noteLength;
		StringBuilder generatedString = new StringBuilder();;
		double measure;
		
		
		for(int k=0; k < newTrackModel.measuresPerPhrase; k++)
		{
			if(!newTrackModel.harmonic || instrumentNum < 1)
			{
				measure = 1;
				do
				{
					noteValue = naturalNoteValues[random.nextInt(6)] + (octave * SEMI_TONE_COUNT);
					prevValues.add(noteValue);
					noteLength = (1.0/powersOfTwo[random.nextInt(3)]);
					prevLengths.add(noteLength);
					measure -= noteLength;
					
					generatedString.append("[" + noteValue + "]/" + noteLength + " ");
				}
				while(measure > 0);
			}
			else if(newTrackModel.harmonic)
			{
				measure = 1;
				do
				{
					noteValue = prevValues.get(k);
					noteLength = prevLengths.get(k);
					measure -= noteLength;
					
					generatedString.append("[" + (noteValue + harmonicValues[random.nextInt(2)]) + "]/" + noteLength + " ");
				}
				while(measure > 0);
			}
			generatedString.append("| ");
		}
		content = generatedString.toString();
	}
}